#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
#=============================================================================
#     FileName: job.py
#         Desc: Job
#      License: GPL
#       Author: Steve Lemuel
#        Email: wlemuel@hotmail.com
#      Version: 0.0.1
#   LastChange: 2015-09-07 14:12:26
#    CreatedAt: 2015-09-06 00:50:10
#=============================================================================
"""
from config import RSS_URL
from model import News
from dbhelper import DBHelper
from downloader import Downloader, TumblrDownloader


class Job:
    def __init__(self, parser):
        self.downloader = Downloader()
        self.parser = parser

    def run(self):
        self.parser.parse(RSS_URL)
        for i in self.parser.get_news():
            self.downloader.set_task(i)


class ConfirmJob:
    def __init__(self):
        self.downloader = Downloader()
        self.db = DBHelper()

    def run(self):
        for news in self.get_failed_news():
            self.downloader.set_task(news)

    def get_failed_news(self):
        return self.db.select(News, status=-1)


class TumblrParseJob:
    def __init__(self, parser):
        self.downloader = None
        self.parser = parser

    def run(self):
        self.parser.run()


class TumblrDownloadJob:
    def __init__(self):
        self.downloader = TumblrDownloader()

    def run(self):
        self.downloader.run()


if __name__ == '__main__':
    # from downloader import Downloader
    # from newsparser import NewsParser
    from newsparser import TumblrParser

    # # init
    # p = NewsParser()

    # job = Job(p)
    # print 'First run'
    # job.run()
    # #print 'Second run'
    # #job.run()
    # job = ConfirmJob()
    # job.run()

    # p = TumblrParser()

    # job = TumblrParseJob(p)
    # job.run()

    job = TumblrDownloadJob()
    job.run()
